<?php

/**
 * 插件基类
 * @author MrHan<hcj112@163.com>
 */

class AddonsController
{

    protected $view = NULL;
    protected $addon_path = NULL;


    /**
     * 插件构造函数,初始化模板引擎,插件的路径
     *
     */
    public function __construct()
    {
        // 实例化视图类
        $this->view = Think::instance('View');
        // 初始化插件路径
        $this->addon_path = ADDONS_PATH . $this->getClassName() . '/';
    }

    // 获取当前类名
    public function getClassName()
    {
        $class = get_class($this);
        return $class;
    }

    public function display($template = '')
    {

        echo($this->fetch($template));
    }


    public function fetch($templateFile = '')
    {
        if (!is_file($templateFile)) {
            $templateFile = $this->addon_path . 'View/' . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
        }

        if (!is_file($templateFile)) {
            throw new \Exception("模板不存在:$templateFile");
        }

        return $this->view->fetch($templateFile);
    }

    protected function assign($name, $value = '')
    {
        $this->view->assign($name, $value);
        return $this;
    }
}